/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import Model.ModelItem;
import Model.ModelJet;
import Model.ModelPet;
import Model.ModelUser;
import Model.Request.ClientRequest;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import libCore.Config;
import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import share.ShareMacros;

/**
 *
 * @author LinhTA
 */
public class LoadGameController extends Controller{

    private static Logger logger_ = Logger.getLogger(LoadGameController.class);
    
    @Override  
    public void handle(ClientRequest request, HttpServletResponse resp) 
    {
         try {
            prepareHeader(resp);
            
            String uid = request._uid;
            ModelJet modJet = new ModelJet(uid);
            ModelPet modPet = new ModelPet(uid);
            ModelUser modUser = new ModelUser(uid);
            
            JSONObject pets = new JSONObject();
            pets.putAll( modPet.getPets());
            
            JSONObject jets = new JSONObject();
            jets.putAll( modJet.getJets());
             
            JSONObject data = new JSONObject();
            data.putAll(modUser.getInfo(request._fbID, request._meID));
            data.put(ShareMacros.PETLIST, pets);
            data.put(ShareMacros.JETLIST, jets);
//            data.put(ShareMacros.PETLIST, "%PET%");
//            data.put(ShareMacros.JETLIST, "%JET%");
            data.put(ShareMacros.TIME, String.valueOf(utilities.time.UtilTime.getTimeNow()));
            data.putAll(ModelItem.getLifeVSTime(uid, resp));
            data.put(ShareMacros.SUSSCES, "true");
             
//            String dataStr  = data.toJSONString();
//            dataStr = dataStr.replaceAll("%PET%", pets.toString());
//            dataStr = dataStr.replaceAll("%JET%", jets.toString());
            
            out(data.toJSONString(), resp);
            
        } catch (Exception ex) {
            logger_.error("Jet.handle:" + ex.getMessage() + ", Username:" + request._uid.toString(), ex);
        }
    }
    
   
    
}
